﻿using System;
using System.Collections.Generic;

using Microsoft.Coding4Fun.TweetCraft.Wow;

using NUnit.Framework;

namespace Microsoft.Coding4Fun.TweetCraft.Tests
{
    internal class ValueChannel : ValueChannelBase
    {
        public override bool IsAvailable(string key)
        {
            return false;
        }

        public override T ReadValue<T>(string key)
        {
            return default(T);
        }

        protected override void Flush(IEnumerable<KeyValuePair<string, object>> queuedValues)
        {
        }
    }

    [TestFixture]
    public class ValueChannelTest
    {
        private IValueChannel valueChannel;

        [SetUp]
        public void SetUp()
        {
            valueChannel = new ValueChannel();
        }

        [Test]
        public void QueueValueTest()
        {
            const int testValue = 42;

            // Queue a value to be flushed into the channel
            valueChannel.QueueValue("testValue", testValue);
        }

        [Test]
        public void IsQueuedTest()
        {
            const int testValue = 42;

            // Checks that no value has been queued for the key
            Assert.IsFalse(valueChannel.IsQueued("testValue"));

            // Queue a value to be flushed into the channel
            valueChannel.QueueValue("testValue", testValue);

            // Checks that a value is now queued for the key
            Assert.IsTrue(valueChannel.IsQueued("testValue"));
        }

        [Test]
        public void GetQueuedValueTest()
        {
            const int testValue = 42;

            // Queue a value to be flushed into the channel
            valueChannel.QueueValue("testValue", testValue);

            // Get the queued value using its key
            int result = valueChannel.GetQueuedValue<int>("testValue");
            
            // Assert that the returned queued value is equal to the original testValue
            Assert.AreEqual(testValue, result);
        }

        [Test]
        public void FlushTest()
        {
            const int testValue = 42;
            
            // Queue a value to be flushed into the channel
            valueChannel.QueueValue("testValue", testValue);

            // Flush the channel explicitly
            valueChannel.Flush();

            // Assert that the value is not queued anymore
            Assert.IsFalse(valueChannel.IsQueued("testValue"));
        }
    }
}
